package com.jerry.cui.dingtalk.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dingtalk.api.request.OapiV2DepartmentCreateRequest;
import com.dingtalk.api.response.OapiV2DepartmentCreateResponse;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListparentbyuserResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.jerry.cui.dingtalk.service.DingTalkDepartmentService;
import com.taobao.api.ApiException;

@RestController
@RequestMapping("department")
public class DingTalkDepartmentController {

	@Autowired
	DingTalkDepartmentService dingTalkDepartmentService;

	@GetMapping("sublist")
	public OapiV2DepartmentListsubResponse getDepartmentSubList(@RequestParam(required = false) Long deptId,
			@RequestParam(required = false) String language) {
		try {
			return dingTalkDepartmentService.getDepartmentSubList(deptId, language);
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	@GetMapping("get")
	public OapiV2DepartmentGetResponse getDepartment(@RequestParam Long deptId,
			@RequestParam(required = false) String language) {
		try {
			return dingTalkDepartmentService.getDepartment(deptId, language);
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	@PostMapping("create")
	public OapiV2DepartmentCreateResponse createDepartment(@RequestBody OapiV2DepartmentCreateRequest request) {
		try {
			return dingTalkDepartmentService.createDepartment(request);
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@PostMapping("listparentbyuser")
	public OapiV2DepartmentListparentbyuserResponse listparentbyuser(@RequestParam String userid) {
		try {
			return dingTalkDepartmentService.listparentbyuser(userid);
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}

}
